<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/25 0025
 * Time: 16:25
 */

namespace app\admin\controller;


class Uploadify extends Base
{
    /**
     * 图片上传
     * @param string $path
     */
    public function upload($path = '') {
        $path = !empty($path) ? $path : 'upload';
        // 获取表单上传文件
        $file = $this->request->file('file');
        $config = [
            'size'=> 1048576, //2M
            'ext'=>'jpg,png,gif,jpeg'
        ];
        $info = $file->validate($config)->move(UPLOAD_PATH . $path);
        if ($info) {
            $saveName = str_replace('\\', '/', $info->getSaveName());
            $data = [
                'code'=> 1,
                'name' => $info->getSaveName(),
                'pic' => '/' . UPLOAD_PATH . $path . '/' . $saveName,
            ];
            exit(json_encode($data));
        }else {
            // 上传失败获取错误信息
            exit(json_encode(['code'=> 0, 'msg' => $file->getError()]));
        }
    }
}